<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>解构默认值</h1>
    <p>仅在：解构失败的时候，才会使用默认的解构对象。</p>
    <script>
        function sum({x,y} = {x:0,y: 0}) {
            console.log("x = ",x,"y = ",y);
            console.log(x + y);
        }

        // let object = {x:1,y:1};
        sum({x:1,y:1}); // 2
        sum(); // 0
        sum({x:1,y: undefined}); // NaN

        sum([]); // NaN
        sum({}); // NaN

        sum(0); // 基本数据类型，会自动转为包装对象进行解构
        sum(false); // NaN

        console.log("------------------分隔符-----------------")
        function sum1({x = 0,y = 0}) {
            console.log("x = ",x,"y = ",y);
            console.log(x + y);
        }

        sum1({}); // 0
        sum1({x:1}); // 1

        function sum2({x = 0,y = 0} = {x:1,y:1}) {
            console.log("x = ",x,"y = ",y);
            console.log(x + y);
        }

        console.log("------------------分隔符-----------------")
        sum2(); // 2
        sum2({x:3}); // 3

    </script>
</body>
</html>